#ifndef _USART1_H
#define _USART1_H
#include "air32f10x_conf.h"

// 端口定义
#define IO_RS485_R0T1 PCout(14)

// 外部变量
extern volatile uint16_t RxMsgBackup;
extern volatile uint8_t NewMsgFlag;
extern uint16_t *const TxCmdHead;
extern uint8_t *const TxCmdID;
extern uint16_t *const TxCmdData;
extern uint16_t *const RxCmdHead;
extern uint8_t *const RxCmdID;
extern uint16_t *const RxCmdData;
extern volatile uint8_t RxBuf[5];
extern volatile uint8_t TxBuf[5];

// 常量
#define BAUD_1M 1000000
#define BAUD_4M 4000000

// 内联函数
__STATIC_FORCEINLINE void Transmit_1byte(uint8_t data)
{
    while ((USART1->SR & USART_FLAG_TXE) == 0)
    {
    }
    USART1->DR = data;
}

__STATIC_FORCEINLINE void usart1_clear_all_flag()
{
    (void)USART1->SR;
    (void)USART1->DR;
}

// 函数声明
void usart1_init_without_IRQ(uint32_t bound);
void usart1_init_with_RXNE(uint32_t bound);
void usart1_transmit_data_by_485(uint8_t *CmdBuf, uint8_t CmdSize);
void usart1_transmit_curtain_cmd();
uint32_t usart1_read_data(uint8_t *buf, uint32_t data_size, uint32_t timeoutms);
void USART1_SendMsg_By_485_Without_Stopbit(uint8_t data);

#endif